//
//  CZVistorView.swift
//  新浪微博
//
//  Created by apple on 15/11/23.
//  Copyright © 2015年 0523. All rights reserved.
//

import UIKit
protocol CZVistorViewDelegate:NSObjectProtocol{
    func vief()
    
    
}
class CZVistorView: UIView {
    weak var delegate:CZVistorViewDelegate?
    required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
    }
    
        
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        prepareui()
    }
    func rotationanimation(){
        let rotation=CABasicAnimation(keyPath:"transform.rotation" )
        rotation.toValue=2*M_PI
        rotation.duration=20
        rotation.repeatCount=MAXFLOAT
        rotation.removedOnCompletion=false
        iconView.layer.addAnimation(rotation, forKey: "rotation")
        
    }
    func pauseAnimation() {
        // 记录暂停时间
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        // 设置动画速度为0
        iconView.layer.speed = 0
        
        // 设置动画偏移时间
        iconView.layer.timeOffset = pauseTime
    }
    func resumeAnimation() {
        // 获取暂停时间
        let pauseTime = iconView.layer.timeOffset
        
        // 设置动画速度为1
        iconView.layer.speed = 1
        
        iconView.layer.timeOffset = 0
        
        iconView.layer.beginTime = 0
        
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        iconView.layer.beginTime = timeSincePause
    }
    func setupvistorview(imagename:String,message:String){
        iconView.image=UIImage(named: imagename)
        messagelable.text=message
        homeView.hidden=true
        coverview.hidden=true
    }
    private func prepareui(){
        addSubview(iconView)
        addSubview(coverview)
        addSubview(homeView)
        addSubview(messagelable)
        addSubview(registerbutton)
        addSubview(loginbutton)
        iconView.translatesAutoresizingMaskIntoConstraints=false
        coverview.translatesAutoresizingMaskIntoConstraints=false
        homeView.translatesAutoresizingMaskIntoConstraints=false
        messagelable.translatesAutoresizingMaskIntoConstraints=false
        registerbutton.translatesAutoresizingMaskIntoConstraints=false
        loginbutton.translatesAutoresizingMaskIntoConstraints=false

        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        /// iconView CenterY与父控件的CenterY重合
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        // 小房子
        // x
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // y
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        // 消息label
        // x
        addConstraint(NSLayoutConstraint(item: messagelable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // y
        addConstraint(NSLayoutConstraint(item: messagelable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        
        addConstraint(NSLayoutConstraint(item: messagelable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 226))
        
        
         addConstraint(NSLayoutConstraint(item: registerbutton,  attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messagelable, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerbutton,  attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messagelable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
      
       
        
        addConstraint(NSLayoutConstraint(item: registerbutton,  attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        
    addConstraint(NSLayoutConstraint(item: registerbutton,  attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        addConstraint(NSLayoutConstraint(item:loginbutton,  attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messagelable, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginbutton,  attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messagelable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        
        
        addConstraint(NSLayoutConstraint(item: loginbutton,  attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        
        addConstraint(NSLayoutConstraint(item: loginbutton,  attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        
        addConstraint(NSLayoutConstraint(item: coverview, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
        // 上
        addConstraint(NSLayoutConstraint(item: coverview, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        // 右
        addConstraint(NSLayoutConstraint(item: coverview, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        // 下
        addConstraint(NSLayoutConstraint(item: coverview, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem:registerbutton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
    }

      private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
  private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var messagelable:UILabel={
       let lable=UILabel()
        
        lable.textColor=UIColor.darkGrayColor()
        lable.text="直到世界尽头"
        lable.numberOfLines=0
        lable.sizeToFit()
        
    return lable
}()
    private lazy var registerbutton:UIButton={
       let button=UIButton()
        button.setBackgroundImage(UIImage(named:"common_button_white_disable" ), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState:UIControlState.Normal )
        button.setTitle("注册", forState: UIControlState.Normal)
         button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.sizeToFit()
        return button
    }()
    private lazy var loginbutton:UIButton={
        let button=UIButton()
         button.setBackgroundImage(UIImage(named:"common_button_white_disable" ), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button.setTitle("登录", forState:UIControlState.Normal )
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.sizeToFit()
        return button
        
    }()
    private lazy var coverview:UIImageView=UIImageView(image:UIImage(named: "visitordiscover_feed_mask_smallicon") )
    
    
    
    
}